import os
from service.sjxz import zddr_upload


def run(llq, zb, ksrq, jsrq, folderpath):
  os.makedirs(folderpath, exist_ok=True)
  browser, context, page = llq
  page.click('#toolbar_header_function')
  page.fill('#search', '凭证导出')
  page.wait_for_timeout(2000)
  try:
      page.wait_for_selector('ul.search--result-list', timeout=2000)
  except Exception:
      page.click('.header--search')
  page.locator('ul.search--result-list li').first.click()
  
  page.click('[formcontrolname="ledgerName"] .f-icon-lookup')
  page.locator('.lookup-tabs-content [name="input-group-value"]').fill(zb)
  page.locator('.lookup-tabs-content [name="input-group-value"]').press('Enter')
  page.wait_for_timeout(1000)
  page.locator('[id^=rownode] .align-items-center span:text-matches(\"^'+zb+'$\")').last.dblclick()

  page.click('[formcontrolname="year"] .f-icon-lookup')
  page.locator('.lookup-tabs-content [title="'+ksrq[:4]+'"]').dblclick()

  page.click('[formcontrolname="filterConditionPlay"] .f-icon-lookup')
  page.wait_for_selector('[field="schemeName"] grid-body-cell')
  try:
     page.wait_for_selector('[field="schemeName"] grid-body-cell span:text-matches(\"^机器人方案$\")', timeout=500)
     page.click('[field="schemeName"] grid-body-cell span:text-matches(\"^机器人方案$\")')
     page.click('.modal-content #button-edit')
     page.click('#content-splitter .f-toolbar button:text-matches("删除行")')
     page.wait_for_timeout(500)
     page.click('.farris-messager .btn-primary')
     page.wait_for_timeout(500)
     page.click('#content-splitter .f-toolbar button:text-matches("删除行")')
     page.wait_for_timeout(500)
     page.click('.farris-messager .btn-primary')
  except Exception:
     page.click('.modal-content #button-add')
     page.wait_for_timeout(500)
     page.fill('.modal-content [formcontrolname="schemeName"]', '机器人方案')
  
  page.click('#content-splitter .f-toolbar button:text-matches("新增行")')
  page.wait_for_timeout(1000)
  page.locator('[field="filterField"]').last.click()
  page.select_option('select.custom-select', value='AccDocDate')
  page.locator('[field="compareType"]').last.click()
  page.select_option('select.custom-select', value='GreateOrEauql')
  page.locator('[field="valuePlay"]').last.click()
  page.fill('[field="valuePlay"] input', ksrq)
  page.locator('[field="filterRelation"]').last.click()
  page.select_option('select.custom-select', value='And')

  page.click('#content-splitter .f-toolbar button:text-matches("新增行")')
  page.wait_for_timeout(1000)
  page.locator('[field="filterField"]').last.click()
  page.select_option('select.custom-select', value='AccDocDate')
  page.locator('[field="compareType"]').last.click()
  page.select_option('select.custom-select', value='LessOrEqual')
  page.locator('[field="valuePlay"]').last.click()
  page.fill('[field="valuePlay"] input', jsrq)
  
  page.click('.modal-content #button-save')
  page.wait_for_timeout(1500)
  page.click('.modal-content #button-sure')
  page.wait_for_timeout(1000)
  with page.expect_download(timeout=600000) as download_info:
    page.locator('#button-add').last.click()
    download = download_info.value
    filepath = os.path.join(folderpath, zb+ksrq+'-'+jsrq+'凭证导出'+download.suggested_filename[-5:])
    download.save_as(filepath)
  zddr_upload.file_uploads(filepath)
  page.click('[title="凭证导出"] div.nav-link-close')




  
